package edu.siu.math.egut.egut;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;


/**
 * All subclasses of this class can be run by 
 * entering: egutEgutFunctionName(arg1,arg2,...) into the egut interpreter.
 * 
 * 
 * @author Joe Pleso
 *
 */
public abstract class AbstractEgutFunction implements Runnable{

    private  String[] data;
    
       
    /**
     * Returns the LiE argument types that this function expects.
     * 
     * @return the LiE argument types that this function expects
     */
    public abstract LiEType[] argList ();
    
    /**
     * Sets the data field
     * 
     * @param in the value to be placed in the data field
     */
    public void setData (String[] in){
	data = in;
	
    }
    
    /**
     * Sets the data field.
     * 
     * @param in the value to be returned to egut/lie
     */
    public void setData (String in){
	data[0] = in;
    }
    
    /**
     * Returns the data field.
     * 
     * @return the data field
     */
    public String[] getRawData(){
	return data;
    }
    
    /**
     * Gets the data for this runnable object. If there was an error with
     * group parsing an appropriate exception will be thrown.
     * 
     * @return the data contained in this runnable object
     * @throws InvocationTargetException 
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     * @throws IllegalArgumentException 
     * @throws NonExceptionalGroupException 
     */
    public AbstractLiEObject[] getData() throws IllegalArgumentException,
    InstantiationException, IllegalAccessException, InvocationTargetException,
    NonExceptionalGroupException{
	
	LiEType[] types = argList();
	AbstractLiEObject[] result = new AbstractLiEObject[types.length];
	
	for (int i = 0; i < types.length; i++){
	    Constructor<?> o = types[i].getConstructors()[0];
	    result[i] = (AbstractLiEObject) o.newInstance(data[i]);
	    if (types[i].equals(LiEType.GRP) 
		    && result[i].getJavaObject() == null){
		data[0] = "\"Specified group was not exceptional.\"";
		throw new NonExceptionalGroupException(result[i].toString());
	    }
	    
	}
	
	
	return result;
    }
    
    
   
    
}
